//
//  CourseCenterOrderView.m
//  xpkc
//
//  Created by TopSageOSX on 15/2/4.
//  Copyright (c) 2015年 TopSage. All rights reserved.
//

#import "CourseCenterOrderView.h"
#import "priceModel.h"
#import "CartModel.h"
#import "youhuiModel.h"
#import "UserMessage.h"
#import "MBProgressHUD.h"
@implementation CourseCenterOrderView
{
   UIScrollView *scroll;
   
   UILabel *recipient;
   UILabel *phoneNum;
   UILabel *address;
   
   UIView *itemview;
   UIView *studyCardView;
   UIView *studyCardACTView;
   UIView *goldcoinView;
   UIView *couponView;
   UIView *infoView;
   
   CGRect item_view_frame;
   CGRect card_view_frame;
   CGRect cardact_view_frame;
   CGRect goldcoin_view_frame;
   CGRect coupon_view_frame;
   CGRect info_view_frame;
   
   double price;
   
   
   UILabel * _shouldPayLabel;
   
   UIImageView * _phoneView;
   double downPrice;
   priceModel *pModel;
   UIView *payview;
   double downGold;
   UILabel *coinNum3;
   UILabel *label7;
   NSString * credits;
   NSString * downStudycards;
   NSMutableArray * quanArray;
   UITableView * quanTableView;
   NSMutableArray * quanTableArray;
   UIButton *couponBtn;
   UIButton * zhekouButton;
   UILabel *label6;
   UITableView * huodongTableView;
   NSMutableArray * huodongTableArray;
   UIButton * huoButton;
   UILabel *label5;
   UIButton *studycardact;
   
   //生成订单传的参数
   NSString * zcardid;
   NSMutableArray * quanid;
   NSString * quanval;
   NSString * addrid;
   NSMutableArray *priceArray;
   NSString * zcardval;
   
}

- (id)initWithFrame:(CGRect)frame{
   self = [super initWithFrame:frame];
   if (self) {
      [self creatView];
      [self createTableView];
      quanArray = [NSMutableArray array];
      quanTableArray = [NSMutableArray array];
      huodongTableArray = [NSMutableArray array];
      priceArray = [NSMutableArray array];
      quanid = [NSMutableArray array];
   }
   return self;
}
/**
 *  creatview
 */
- (void)creatView{
   
   CGFloat offwith = 0.04*SCREEN_WIDTH;
   CGRect rec_btn_frame = CGRectMake(0, offwith, SCREEN_WIDTH, 80);
   CGRect rec_label_frame = CGRectMake(offwith, offwith, 100, 20);
   CGRect rec_phone_img_frame = CGRectMake(SCREEN_WIDTH-160, offwith, 14, 20);
   CGRect rec_phone_frame = CGRectMake(SCREEN_WIDTH-140, offwith, 120, 20);
   CGRect rec_right_frame = CGRectMake(SCREEN_WIDTH-20, 25, 15, 30);
   CGRect rec_address_frame = CGRectMake(offwith, 38, SCREEN_WIDTH-40, 40);
   
   item_view_frame = CGRectMake(0, CGRectGetMaxY(rec_btn_frame)+offwith, SCREEN_WIDTH, 20);
   card_view_frame = CGRectMake(0, CGRectGetMaxY(item_view_frame)+3, SCREEN_WIDTH, 30);
   cardact_view_frame = CGRectMake(0, CGRectGetMaxY(card_view_frame)+3, SCREEN_WIDTH, 30);
   goldcoin_view_frame = CGRectMake(0, CGRectGetMaxY(cardact_view_frame)+3, SCREEN_WIDTH, 30);
   coupon_view_frame = CGRectMake(0, CGRectGetMaxY(goldcoin_view_frame)+3, SCREEN_WIDTH, 30);
   info_view_frame = CGRectMake(0, CGRectGetMaxY(coupon_view_frame)+3, SCREEN_WIDTH, 200);
   
   CGRect pay_view_frame = CGRectMake(0, SCREEN_HEIGHT-64-60, SCREEN_WIDTH, 60);
   //整体滑动视图
   scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64-60)];
   scroll.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT-64-60);
   [self addSubview:scroll];
   //收货人
   UIButton *recipientView = [UIButton buttonWithType:UIButtonTypeCustom];
   self.recipientBtn = recipientView;
   recipientView.frame = rec_btn_frame;
   [recipientView setBackgroundImage:[UIImage imageNamed:@"addressBG"] forState:UIControlStateNormal];
   [scroll addSubview:recipientView];
   UIImageView *righttip = [[UIImageView alloc]initWithFrame:rec_right_frame];
   righttip.image = [UIImage imageNamed:@"tip"];
   [recipientView addSubview:righttip];
   recipient = [[UILabel alloc]initWithFrame:rec_label_frame];
   recipient.font = FONT_SYS_L;
   [recipientView addSubview:recipient];
   UIImageView *phone = [[UIImageView alloc]initWithFrame:rec_phone_img_frame];
   _phoneView = phone;
   [recipientView addSubview:phone];
   phoneNum = [[UILabel alloc]initWithFrame:rec_phone_frame];
   [recipientView addSubview:phoneNum];
   address = [[UILabel alloc]initWithFrame:rec_address_frame];
   address.numberOfLines = 0;
   address.font = FONT_SYS_M;
   address.textColor = [UIColor lightGrayColor];
   [recipientView addSubview:address];
   
   
   
   //商品详情
//   itemview = [[UIView alloc]initWithFrame:item_view_frame];
   itemview = [[UIView alloc]init];
   itemview.backgroundColor = [UIColor whiteColor];
   [scroll addSubview:itemview];
   
   //抵扣选择
   

   //折扣详情
   infoView = [[UIView alloc]initWithFrame:info_view_frame];
   infoView.backgroundColor = [UIColor whiteColor];
   [scroll addSubview:infoView];
   UILabel *infototal =    [[UILabel alloc]initWithFrame:CGRectMake(-30, 0, 120, 30)];
   infototal.text = @"支付方式";
   infototal.textAlignment = NSTextAlignmentRight;
   infototal.font=[UIFont boldSystemFontOfSize:20];
   [infoView addSubview:infototal];
   UILabel * label2 =[[UILabel alloc] initWithFrame:CGRectMake(120, 0, 120, 30)];
   label2.text=@"支付宝支付";
   label2.font=[UIFont systemFontOfSize:15];
   [infoView addSubview:label2];
   
   NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
   [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
   NSString * time = [formatter stringFromDate:[NSDate date]];
   
   UILabel *label8 =[[UILabel alloc] initWithFrame:CGRectMake(0, 40+30+30+30+30, SCREEN_WIDTH-20, 30)];
   label8.text=[NSString stringWithFormat:@"下单时间： %@",time];
   label8.textAlignment=NSTextAlignmentRight;
   label8.font=[UIFont systemFontOfSize:15];
   label8.textColor=[UIColor grayColor];
   [infoView addSubview:label8];
   
   
   
   
   
   
   //支付方式与实际支付
   payview = [[UIView alloc]initWithFrame:pay_view_frame];
   payview.backgroundColor = [UIColor whiteColor];
   [self addSubview:payview];
   
   UIButton *payBtn = [UIButton buttonWithType:UIButtonTypeCustom];
   payBtn.frame = CGRectMake(SCREEN_WIDTH*2/3,10, SCREEN_WIDTH/3-10, 40);
   payBtn.backgroundColor = UIColorFromRGB(0xff4e4e);
   [payBtn setTitle:@"去结算" forState:UIControlStateNormal];
   payBtn.layer.cornerRadius = 5;
   [payBtn addTarget:self action:@selector(submitOrder) forControlEvents:UIControlEventTouchUpInside];
   [payview addSubview:payBtn];
}



//开关方法
-(void)switchClick:(id)sender
{
   
   UISwitch *switchButton =(UISwitch *)sender;
   BOOL isButton = [switchButton isOn];
   if (isButton) {
      NSLog(@"开");
      
      
      MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
      NSString *module = @"shopcar";
      NSString *interface = @"luntangold";
      NSString *timeString = [GetTime getTime];
      NSString *hashString = [[NSString stringWithFormat:@"%@/%@/key/%@/time/%@",module,interface,KEY,timeString] md5];
      NSString *urlString = [NSString stringWithFormat:@"%@/laravel/public/%@/%@/hash/%@/appid/1",URL2,module,interface,hashString];
      
      NSDictionary *parameter = @{@"time":timeString,@"username":[UserMessage sharedUserMessage].userId};
      //POST请求
      AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
      manager.responseSerializer = [AFHTTPResponseSerializer serializer];
      [manager POST:urlString parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
         NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
         
         if ([[dic objectForKey:@"succ"] isEqualToNumber:@1]) {
            
            
            NSDictionary *dic1 =[dic objectForKey:@"info"];
            credits =[NSString stringWithFormat:@"%@",[dic1 objectForKey:@"credits"]];
            if (downGold==0) {
               [pModel setValue:[NSString stringWithFormat: @"%.02f",price] forKey:@"price"];
               [pModel setValue:@"0" forKey:@"gold"];
            }else{
               if ([credits doubleValue]<=downGold) {
                  
                  
                  
                  [pModel setValue:[NSString stringWithFormat: @"%.02f",price-[credits doubleValue]] forKey:@"price"];
                  [pModel setValue:[NSString stringWithFormat:@"%.02f",[credits doubleValue]] forKey:@"gold"];
                  
                  
               }else{
                  
                  [pModel setValue:[NSString stringWithFormat: @"%.02f",price-downGold] forKey:@"price"];
                  
                  [pModel setValue:[NSString stringWithFormat:@"%.02f",downGold] forKey:@"gold"];
                  
               }
               
               
            }
            
         }
         [hud hide:YES];
      } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"shibai");
         ShowAlertMsg(@"加载失败");
         [hud hide:YES];
      }];
      
   }else{
      NSLog(@"关");
      [pModel setValue:[NSString stringWithFormat: @"%.02f",price] forKey:@"price"];
      [pModel setValue:@"0" forKey:@"gold"];
      
   }
   
}
//KVO方法
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
   
   if ([keyPath isEqualToString:@"price"]) {
      
      _shouldPayLabel.text=[NSString stringWithFormat:@"应支付: %.02f",[[pModel valueForKey:@"price"] doubleValue]-[downStudycards doubleValue]-[[pModel valueForKey:@"quan"] doubleValue]];
      
   }
   if ([keyPath isEqualToString:@"gold"]) {
      label7.text=[NSString stringWithFormat:@"- 使用大家网论坛金币：￥%@",[pModel valueForKey:@"gold"]];
   }
   if ([keyPath isEqualToString:@"quan"]) {
      label6.text=[NSString stringWithFormat:@"- 使用优惠劵：￥%@",[pModel valueForKey:@"quan"]];
      _shouldPayLabel.text=[NSString stringWithFormat:@"应支付: %.02f",[[pModel valueForKey:@"price"] doubleValue]-[downStudycards doubleValue]-[[pModel valueForKey:@"quan"] doubleValue]- [[pModel valueForKey:@"huo"] doubleValue]];
   }
   if ([keyPath isEqualToString:@"huo"]) {
      label5.text=[NSString stringWithFormat:@"- 使用活动学习卡：￥%@",[pModel valueForKey:@"huo"]];
      _shouldPayLabel.text=[NSString stringWithFormat:@"应支付: %.02f",[[pModel valueForKey:@"price"] doubleValue]-[downStudycards doubleValue]-[[pModel valueForKey:@"quan"] doubleValue]- [[pModel valueForKey:@"huo"] doubleValue]];
   }
   
   
}
-(void)dealloc
{
   //移除监听
   [pModel removeObserver:self forKeyPath:@"price"];
   [pModel removeObserver:self forKeyPath:@"gold"];
   [pModel removeObserver:self forKeyPath:@"quan"];
   [pModel removeObserver:self forKeyPath:@"huo"];
}




//优惠劵处理
-(void)createTableView{
   quanTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
   quanTableView.hidden = YES;
   quanTableView.delegate = self;
   quanTableView.dataSource = self;
   quanTableView.tag=10001;
   [scroll addSubview:quanTableView];
   
   huodongTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
   huodongTableView.hidden = YES;
   huodongTableView.delegate = self;
   huodongTableView.dataSource = self;
   huodongTableView.tag=10002;
   [scroll addSubview:huodongTableView];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   if (tableView.tag==10001) {
      return  quanTableArray.count;
   }else{
      
      return huodongTableArray.count;
   }
   
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:@"cell"];
   if (!cell) {
      cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
   }
   
   if (tableView.tag==10001) {
      youhuiModel * model = quanTableArray[indexPath.row];
      cell.textLabel.font=[UIFont boldSystemFontOfSize:10];
      cell.textLabel.text=model.name;
   }else{
      youhuiModel * model = huodongTableArray[indexPath.row];
      cell.textLabel.font=[UIFont boldSystemFontOfSize:10];
      cell.textLabel.text=model.c_sn;
   }
   
   return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   if (tableView.tag==10001) {
      quanTableView.hidden=YES;
      youhuiModel * model = quanTableArray[indexPath.row];
      [zhekouButton setTitle:[NSString stringWithFormat:@"%@",model.name] forState:UIControlStateNormal];
      [pModel setValue:model.subtract_price forKey:@"quan"];
      huoButton.selected=NO;
      zhekouButton.selected=NO;
      [quanid addObject:model.quanid];
      quanval=model.subtract_price;
   }else
   {
      huodongTableView.hidden=YES;
      youhuiModel * model = huodongTableArray[indexPath.row];
      [huoButton setTitle:[NSString stringWithFormat:@"%@",model.c_sn] forState:UIControlStateNormal];
      [pModel setValue:model.c_value forKey:@"huo"];
      huoButton.selected=NO;
      zhekouButton.selected=NO;
      zcardid=model.c_mid;
      zcardval=model.c_value;
   }
   
   
}
-(void)getyouhuijuanData
{
   //折扣卷数据请求
   MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
   hud.labelText = @"加载中";
   
   NSString *module = @"shopcar";
   NSString *interface = @"quanlist";
   
   NSString *timeString = [GetTime getTime];
   NSString *hashString = [[NSString stringWithFormat:@"%@/%@/key/%@/time/%@",module,interface,KEY,timeString] md5];
   NSString *urlString = [NSString stringWithFormat:@"%@/laravel/public/%@/%@/hash/%@/appid/1",URL2,module,interface,hashString];
   NSDictionary *parameter = @{@"time":timeString,@"mid":[UserMessage sharedUserMessage].userMid,@"goods_list":quanArray};
   
   //POST请求
   AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
   manager.responseSerializer = [AFHTTPResponseSerializer serializer];
   [manager POST:urlString parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
      
      NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
      
      
      if ([[dic objectForKey:@"succ"]isEqualToNumber:@1]) {
         NSArray * infoArray=[dic objectForKey:@"info"];
         for (NSDictionary * infoDic in infoArray) {
            youhuiModel * model =[[youhuiModel alloc] init];
            model.name=[infoDic objectForKey:@"name"];
            model.parallel_use=[infoDic objectForKey:@"parallel_use"];
            model.subtract_price=[infoDic objectForKey:@"subtract_price"];
            model.quanid=[infoDic objectForKey:@"id"];
            [quanTableArray addObject:model];
         }
         [hud hide:YES];
         
      }
      
      
      [hud hide:YES];
   } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
      
      [hud hide:YES];
      
      
      
      
   }];
   
}
-(void)gethuodongCard
{
   
   NSString *module = @"shopcar";
   NSString *interface = @"activestudycardlists";
   
   NSString *timeString = [GetTime getTime];
   NSString *hashString = [[NSString stringWithFormat:@"%@/%@/key/%@/time/%@",module,interface,KEY,timeString] md5];
   NSString *urlString = [NSString stringWithFormat:@"%@/laravel/public/%@/%@/hash/%@/appid/1",URL2,module,interface,hashString];
   NSDictionary *parameter = @{@"time":timeString,@"mid":[UserMessage sharedUserMessage].userMid,@"goods_list":quanArray};
   
   //POST请求
   AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
   manager.responseSerializer = [AFHTTPResponseSerializer serializer];
   [manager POST:urlString parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
      
      NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
      
      if ([[dic objectForKey:@"succ"]isEqualToNumber:@1]) {
         NSArray * infoArray=[dic objectForKey:@"info"];
         
         for (NSDictionary * dic1  in infoArray) {
            youhuiModel * model =[[youhuiModel alloc] init];
            model.c_sn=[dic1 objectForKey:@"c_sn"];
            model.c_value=[dic1 objectForKey:@"c_value"];
            model.c_mid=[dic1 objectForKey:@"cid"];
            [huodongTableArray addObject:model];
            
         }
         
      }
      
      
      
   } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
      
      
      
      
      
      
   }];
   
}

//收货地址处理
- (void)setRecipitentInfoWithDic:(NSDictionary *)info{
   
   addrid = [info objectForKey:@"id"];
   
   if (info.count == 0) {
      
      [self.recipientBtn setTitle:@"您还没添加收货地址" forState:UIControlStateNormal];
      
      [self.recipientBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
      recipient.text = @"";
      phoneNum.text = @"";
      address.text = @"";
      _phoneView.image = [UIImage imageNamed:@""];
      
   }else{
      [self.recipientBtn setTitle:@" " forState:UIControlStateNormal];
      recipient.text = info[@"consignee"];
      phoneNum.text = info[@"tel"];
      address.text = [NSString stringWithFormat:@"%@%@",info[@"address"],info[@"des"]];
      _phoneView.image = [UIImage imageNamed:@"phone"];
   }
   
}


/**
 *  设置购物信息
 *
 *  @param items items
 */




- (void)setItemInfoWithArray:(NSArray *)items price:(double)currentPrice studycard:(NSString *)studycard{
   
   downStudycards=studycard;
   price=currentPrice;
   itemview.height = items.count *80+2;
   for (int i = 0; i<items.count; i++) {
      CartModel * model = items[i];
      CourseCenterCartCell *cell = [[CourseCenterCartCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
      cell.frame = CGRectMake(0, i*80, SCREEN_WIDTH, 80);
      cell.editing = NO;
      cell.layer.borderWidth = 0.3;
      cell.layer.borderColor = [UIColor lightGrayColor].CGColor;
      cell.itemTitle.text = model.title;
      cell.itemPrice.text = [NSString stringWithFormat:@"￥：%@",model.price];
      
      
      // cell.itemIntroduction.text = @"赠：好多好多\n好多好东西呢";
      cell.userInteractionEnabled = NO;
      [itemview addSubview:cell];
      downGold+=[model.gold doubleValue];
      [quanArray addObject:model.quanID];
      [priceArray addObject:model.price];
      
//      NSDictionary * dic = _dataSource[indexPath.row];
//      CGFloat offwidth = 0.04*SCREEN_WIDTH;
//      
//      CGFloat viewH = offwidth * 0.5 + 60;
//      if (dic.count > 4) {
//         
//         NSArray * wardArray = dic[@"ward"];
//         [cell configWithArray:wardArray];
//         
//         //        cell.lineLabel.frame = CGRectMake(0, offwidth * 0.5 +60+10 + 60 * wardArray.count , SCREEN_WIDTH, 1);
//         viewH += wardArray.count * 50 + 20;
//      }else{
//         
//         //        cell.lineLabel.frame = CGRectMake(0, offwidth * 0.5 +59, SCREEN_WIDTH - 50 , 1);
//      }

      
      
   }
   
   
   [self getyouhuijuanData];
   [self gethuodongCard];
   
   
   
   
   
   //学习卡
   studyCardView = [[UIView alloc]initWithFrame:card_view_frame];
   studyCardView.backgroundColor = [UIColor whiteColor];
   studyCardView.clipsToBounds = YES;
   [scroll addSubview:studyCardView];
   
   //金币
   goldcoinView = [[UIView alloc]initWithFrame:goldcoin_view_frame];
   goldcoinView.backgroundColor = [UIColor whiteColor];
   goldcoinView.clipsToBounds = YES;
   [scroll addSubview:goldcoinView];
   
   
   //活动学习卡
   studyCardACTView = [[UIView alloc]initWithFrame:cardact_view_frame];
   studyCardACTView.backgroundColor = [UIColor whiteColor];
   studyCardACTView.clipsToBounds = YES;
   studyCardACTView.userInteractionEnabled=YES;
   [scroll addSubview:studyCardACTView];
   
   
   
   //抵扣券
   couponView = [[UIView alloc]initWithFrame:coupon_view_frame];
   couponView.backgroundColor = [UIColor whiteColor];
   couponView.clipsToBounds = YES;
   [scroll addSubview:couponView];
   
   
   
   UILabel *label4 =[[UILabel alloc] initWithFrame:CGRectMake(0, 40+30+30+30, SCREEN_WIDTH-20, 30)];
   label4.text=[NSString stringWithFormat:@"- 使用学习卡金额：￥%@",studycard];
   label4.textAlignment=NSTextAlignmentRight;
   [infoView addSubview:label4];
   
   UILabel *studycardbtn = [[UILabel alloc] initWithFrame: CGRectMake(10, 0, SCREEN_WIDTH-60, 40)];
   studycardbtn.font=[UIFont systemFontOfSize:17];
   studycardbtn.text=[NSString stringWithFormat:@"学习卡:         余额:    ￥%@",studycard];
   studycardbtn.textColor=[UIColor blackColor];
   [studyCardACTView addSubview:studycardbtn];
   self.studyButton =[[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-60, 0, 60, 30)];
   self.studyButton.userInteractionEnabled=YES;
   [self.studyButton setTitle:@"立即充值" forState:(UIControlStateNormal)];
   [self.studyButton setTitleColor:OrangeColor forState:(UIControlStateNormal)];
   self.studyButton.titleLabel.font=[UIFont boldSystemFontOfSize:15];
   [self.studyButton addTarget:self action:@selector(studyButton2:) forControlEvents:UIControlEventTouchUpInside];
   [studyCardACTView addSubview:self.studyButton];
   
   
   UILabel *coinNum = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH/3+10, 30)];
   coinNum.text = @"大家论坛金币抵扣：";
   coinNum.font=[UIFont systemFontOfSize:15];
   UILabel * coinNum2 =[[UILabel alloc] initWithFrame:CGRectMake(10+SCREEN_WIDTH/3+10, 0, 70, 30)];
   coinNum2.text=@"最多可使用";
   coinNum2.font=[UIFont systemFontOfSize:12];
   [studyCardView addSubview:coinNum];
   [studyCardView addSubview:coinNum2];
   //大家论坛金币处理
   coinNum3=[[UILabel alloc] initWithFrame:CGRectMake(10+SCREEN_WIDTH/3+10+60, 0, 50, 30)];
   coinNum3.text=[NSString stringWithFormat:@"￥%d",(int)downGold];
   coinNum3.textColor=[UIColor redColor];
   coinNum3.font=[UIFont systemFontOfSize:12];
   UISwitch *swit = [[UISwitch alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-70, 0, 50, 20)];
   [swit addTarget:self action:@selector(switchClick:) forControlEvents:(UIControlEventTouchUpInside)];
   [studyCardView addSubview:swit];
   [studyCardView addSubview:coinNum3];
   
   
   
   
   UILabel *stud = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH/3, 30)];
   stud.text=@"活动学习卡：";
   stud.textColor=[UIColor blackColor];
   [goldcoinView addSubview:stud];
   huoButton = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/3+10, 0, 100, 30)];
   huoButton.layer.cornerRadius=10;
   huoButton.layer.masksToBounds=YES;
   huoButton.layer.borderColor=[UIColor blackColor].CGColor;
   huoButton.layer.borderWidth=1;
   [huoButton setTitle:@"请选择活动学习卡" forState:(UIControlStateNormal)];
   [huoButton setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
   huoButton.titleLabel.font=[UIFont boldSystemFontOfSize:12];
   [huoButton addTarget:self action:@selector(showorhidecardact:) forControlEvents:UIControlEventTouchUpInside];
   [goldcoinView addSubview:huoButton];
   UILabel *yourcardact = [[UILabel alloc]initWithFrame:CGRectMake(0, 30, SCREEN_WIDTH, 20)];
   yourcardact.text = @"您目前共有0张活动学习卡可用";
   [goldcoinView addSubview:yourcardact];
   self.studyButton2 =[[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-60, 0, 60, 30)];
   [self.studyButton2 setTitle:@"立即充值" forState:(UIControlStateNormal)];
   [self.studyButton2 setTitleColor:OrangeColor forState:(UIControlStateNormal)];
   self.studyButton2.titleLabel.font=[UIFont boldSystemFontOfSize:15];
   [self.studyButton2 addTarget:self action:@selector(studyButton2:) forControlEvents:UIControlEventTouchUpInside];
   [goldcoinView addSubview:self.studyButton2];
   
   
   
   
   UILabel  *couponLab= [[UILabel alloc] initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH/3, 30)];
   couponLab.text=@"优惠券:";
   couponLab.textColor=[UIColor blackColor];
   [couponView addSubview:couponLab];
   zhekouButton= [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/3+10, 0, 100, 30)];
   zhekouButton.layer.cornerRadius=10;
   zhekouButton.layer.masksToBounds=YES;
   zhekouButton.layer.borderColor=[UIColor blackColor].CGColor;
   zhekouButton.layer.borderWidth=1;
   [zhekouButton setTitle:@"请选择使用优惠劵" forState:(UIControlStateNormal)];
   [zhekouButton setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
   zhekouButton.titleLabel.font=[UIFont boldSystemFontOfSize:12];
   zhekouButton.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
   [zhekouButton addTarget:self action:@selector(showorhidecoupon:) forControlEvents:UIControlEventTouchUpInside];
   [couponView addSubview:zhekouButton];
   UILabel *yourcoupon = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/3, 30, SCREEN_WIDTH/2, 20)];
   yourcoupon.text = @"您没有可以使用的折扣券";
   [couponView addSubview:yourcoupon];
   
   
   
   
   
   
   //kvo处理
   pModel =[[priceModel alloc] init];
   [pModel setValue:[NSString stringWithFormat: @"%.02f",currentPrice] forKey:@"price"];
   [pModel addObserver:self forKeyPath:@"price" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
   
   [pModel setValue:@"0" forKey:@"gold"];
   [pModel addObserver:self forKeyPath:@"gold" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
   
   
   
   [pModel setValue:@"0" forKey:@"quan"];
   [pModel addObserver:self forKeyPath:@"quan" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
   
   
   [pModel setValue:@"0" forKey:@"huo"];
   [pModel addObserver:self forKeyPath:@"huo" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
   
   _shouldPayLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 200, 40)];
   _shouldPayLabel.text=[NSString stringWithFormat:@"应支付: %.02f",[[pModel valueForKey:@"price"] doubleValue]-[studycard doubleValue]- [[pModel valueForKey:@"quan"] doubleValue]];
   _shouldPayLabel.textColor=[UIColor redColor];
   
   label7 =[[UILabel alloc] initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH-20, 30)];
   label7.textAlignment=NSTextAlignmentRight;
   label7.text=[NSString stringWithFormat:@"- 使用大家网论坛金币：￥%@",[pModel valueForKey:@"gold"]];
   
   label6=[[UILabel alloc] initWithFrame:CGRectMake(0, 40+30+30, SCREEN_WIDTH-20, 30)];
   label6.text=[NSString stringWithFormat:@"- 使用优惠劵：￥%@",[pModel valueForKey:@"quan"]];
   label6.textAlignment=NSTextAlignmentRight;
   
   
   label5 =[[UILabel alloc] initWithFrame:CGRectMake(0, 40+30, SCREEN_WIDTH-20, 30)];
   label5.text=[NSString stringWithFormat:@"- 使用活动学习卡：￥%@",[pModel valueForKey:@"huo"]];
   label5.textAlignment=NSTextAlignmentRight;
   
   [infoView addSubview:label5];
   [infoView addSubview:label6];
   [infoView addSubview:label7];
   [payview addSubview:_shouldPayLabel];
   
   item_view_frame = CGRectMake(0, 0.08*SCREEN_WIDTH+80, SCREEN_WIDTH, 80*items.count);
   [self updateItemFrame];
   
}


- (void)setItemInfoWithArray:(NSArray *)items price:(double)currentPrice studycard:(NSString *)studycard array:(NSArray *)dictArray{
   
   
   
   
   downStudycards=studycard;
   price=currentPrice;
//   itemview.height = items.count *80+2;
   
   
   CGFloat cartH = dictArray.count * 80 + 2;
   CGFloat cellH = 0;
   for (int i = 0; i<items.count; i++) {
      CartModel * model = items[i];
      CourseCenterCartCell *cell = [[CourseCenterCartCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
      
      cell.editing = NO;
      cell.layer.borderWidth = 0.3;
      cell.layer.borderColor = [UIColor lightGrayColor].CGColor;
      cell.itemTitle.text = model.title;
      cell.itemPrice.text = [NSString stringWithFormat:@"￥：%@",model.price];
      
      
      // cell.itemIntroduction.text = @"赠：好多好多\n好多好东西呢";
      cell.userInteractionEnabled = NO;
      [itemview addSubview:cell];
      downGold+=[model.gold doubleValue];
      [quanArray addObject:model.quanID];
      [priceArray addObject:model.price];
      
      NSLog(@"%@",dictArray);
      
      NSDictionary * dict = dictArray[i];
      
      CGFloat viewH = 0;
      if (dict.count > 4) {

         
         
         NSArray * wardArray = dict[@"ward"];
         
         
         [cell configWithArray:wardArray];
         viewH = wardArray.count * 50;
         
      }


      
      
      cartH += viewH;

      cell.frame = CGRectMake(0, cellH , SCREEN_WIDTH, 80+viewH);
      cellH += 80;
      cellH += viewH;
      
//      cell.lineLabel.frame = CGRectMake(0, rect.origin.y, SCREEN_WIDTH, 1);
//      cell.lineLabel.backgroundColor = [UIColor whiteColor];
      [cell.lineLabel removeFromSuperview];
   }
   
   
//   CGFloat offwith = 0.04*SCREEN_WIDTH;
//   CGRect rec_btn_frame = CGRectMake(0, offwith, SCREEN_WIDTH, cartH + dictArray.count * 80);
//   item_view_frame = CGRectMake(0, CGRectGetMaxY(rec_btn_frame)+offwith, SCREEN_WIDTH, 20);
   card_view_frame = CGRectMake(0, CGRectGetMaxY(item_view_frame)+3, SCREEN_WIDTH, 30);
   cardact_view_frame = CGRectMake(0, CGRectGetMaxY(card_view_frame)+3, SCREEN_WIDTH, 30);
   goldcoin_view_frame = CGRectMake(0, CGRectGetMaxY(cardact_view_frame)+3, SCREEN_WIDTH, 30);
   coupon_view_frame = CGRectMake(0, CGRectGetMaxY(goldcoin_view_frame)+3, SCREEN_WIDTH, 30);
   info_view_frame = CGRectMake(0, CGRectGetMaxY(coupon_view_frame)+3, SCREEN_WIDTH, 200);
   
   itemview.height = cartH;
   
   
   itemview.frame = item_view_frame;
   studyCardView.frame = card_view_frame;
   goldcoinView.frame = goldcoin_view_frame;
   studyCardACTView.frame = cardact_view_frame;
   couponView.frame = coupon_view_frame;
   
   
   [self getyouhuijuanData];
   [self gethuodongCard];
   
   
   
   
   
   //学习卡
//   studyCardView = [[UIView alloc]initWithFrame:card_view_frame];
   studyCardView = [[UIView alloc]init];
   studyCardView.backgroundColor = [UIColor whiteColor];
   studyCardView.clipsToBounds = YES;
   [scroll addSubview:studyCardView];
   
   //金币
//   goldcoinView = [[UIView alloc]initWithFrame:goldcoin_view_frame];
   goldcoinView = [[UIView alloc]init];
   goldcoinView.backgroundColor = [UIColor whiteColor];
   goldcoinView.clipsToBounds = YES;
   [scroll addSubview:goldcoinView];
   
   
   //活动学习卡
//   studyCardACTView = [[UIView alloc]initWithFrame:cardact_view_frame];
   studyCardACTView = [[UIView alloc]init];
   studyCardACTView.backgroundColor = [UIColor whiteColor];
   studyCardACTView.clipsToBounds = YES;
   studyCardACTView.userInteractionEnabled=YES;
   [scroll addSubview:studyCardACTView];
   
   
   
   //抵扣券
//   couponView = [[UIView alloc]initWithFrame:coupon_view_frame];
   couponView = [[UIView alloc]init];
   couponView.backgroundColor = [UIColor whiteColor];
   couponView.clipsToBounds = YES;
   [scroll addSubview:couponView];
   
   
   
   UILabel *label4 =[[UILabel alloc] initWithFrame:CGRectMake(0, 40+30+30+30, SCREEN_WIDTH-20, 30)];
   label4.text=[NSString stringWithFormat:@"- 使用学习卡金额：￥%@",studycard];
   label4.textAlignment=NSTextAlignmentRight;
   [infoView addSubview:label4];
   
   UILabel *studycardbtn = [[UILabel alloc] initWithFrame: CGRectMake(10, 0, SCREEN_WIDTH-60, 40)];
   studycardbtn.font=[UIFont systemFontOfSize:17];
   studycardbtn.text=[NSString stringWithFormat:@"学习卡:         余额:    ￥%@",studycard];
   studycardbtn.textColor=[UIColor blackColor];
   [studyCardACTView addSubview:studycardbtn];
   self.studyButton =[[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-60, 0, 60, 30)];
   self.studyButton.userInteractionEnabled=YES;
   [self.studyButton setTitle:@"立即充值" forState:(UIControlStateNormal)];
   [self.studyButton setTitleColor:OrangeColor forState:(UIControlStateNormal)];
   self.studyButton.titleLabel.font=[UIFont boldSystemFontOfSize:15];
   [self.studyButton addTarget:self action:@selector(studyButton2:) forControlEvents:UIControlEventTouchUpInside];
   [studyCardACTView addSubview:self.studyButton];
   
   
   UILabel *coinNum = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH/3+10, 30)];
   coinNum.text = @"大家论坛金币抵扣：";
   coinNum.font=[UIFont systemFontOfSize:15];
   UILabel * coinNum2 =[[UILabel alloc] initWithFrame:CGRectMake(10+SCREEN_WIDTH/3+10, 0, 70, 30)];
   coinNum2.text=@"最多可使用";
   coinNum2.font=[UIFont systemFontOfSize:12];
   [studyCardView addSubview:coinNum];
   [studyCardView addSubview:coinNum2];
   //大家论坛金币处理
   coinNum3=[[UILabel alloc] initWithFrame:CGRectMake(10+SCREEN_WIDTH/3+10+60, 0, 50, 30)];
   coinNum3.text=[NSString stringWithFormat:@"￥%d",(int)downGold];
   coinNum3.textColor=[UIColor redColor];
   coinNum3.font=[UIFont systemFontOfSize:12];
   UISwitch *swit = [[UISwitch alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-70, 0, 50, 20)];
   [swit addTarget:self action:@selector(switchClick:) forControlEvents:(UIControlEventTouchUpInside)];
   [studyCardView addSubview:swit];
   [studyCardView addSubview:coinNum3];
   
   
   
   
   UILabel *stud = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH/3, 30)];
   stud.text=@"活动学习卡：";
   stud.textColor=[UIColor blackColor];
   [goldcoinView addSubview:stud];
   huoButton = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/3+10, 0, 100, 30)];
   huoButton.layer.cornerRadius=10;
   huoButton.layer.masksToBounds=YES;
   huoButton.layer.borderColor=[UIColor blackColor].CGColor;
   huoButton.layer.borderWidth=1;
   [huoButton setTitle:@"请选择活动学习卡" forState:(UIControlStateNormal)];
   [huoButton setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
   huoButton.titleLabel.font=[UIFont boldSystemFontOfSize:12];
   [huoButton addTarget:self action:@selector(showorhidecardact:) forControlEvents:UIControlEventTouchUpInside];
   [goldcoinView addSubview:huoButton];
   UILabel *yourcardact = [[UILabel alloc]initWithFrame:CGRectMake(0, 30, SCREEN_WIDTH, 20)];
   yourcardact.text = @"您目前共有0张活动学习卡可用";
   [goldcoinView addSubview:yourcardact];
   self.studyButton2 =[[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-60, 0, 60, 30)];
   [self.studyButton2 setTitle:@"立即充值" forState:(UIControlStateNormal)];
   [self.studyButton2 setTitleColor:OrangeColor forState:(UIControlStateNormal)];
   self.studyButton2.titleLabel.font=[UIFont boldSystemFontOfSize:15];
   [self.studyButton2 addTarget:self action:@selector(studyButton2:) forControlEvents:UIControlEventTouchUpInside];
   [goldcoinView addSubview:self.studyButton2];
   
   
   
   
   UILabel  *couponLab= [[UILabel alloc] initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH/3, 30)];
   couponLab.text=@"优惠券:";
   couponLab.textColor=[UIColor blackColor];
   [couponView addSubview:couponLab];
   zhekouButton= [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/3+10, 0, 100, 30)];
   zhekouButton.layer.cornerRadius=10;
   zhekouButton.layer.masksToBounds=YES;
   zhekouButton.layer.borderColor=[UIColor blackColor].CGColor;
   zhekouButton.layer.borderWidth=1;
   [zhekouButton setTitle:@"请选择使用优惠劵" forState:(UIControlStateNormal)];
   [zhekouButton setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
   zhekouButton.titleLabel.font=[UIFont boldSystemFontOfSize:12];
   zhekouButton.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
   [zhekouButton addTarget:self action:@selector(showorhidecoupon:) forControlEvents:UIControlEventTouchUpInside];
   [couponView addSubview:zhekouButton];
   UILabel *yourcoupon = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/3, 30, SCREEN_WIDTH/2, 20)];
   yourcoupon.text = @"您没有可以使用的折扣券";
   [couponView addSubview:yourcoupon];
   
   
   
   
   
   
   //kvo处理
   pModel =[[priceModel alloc] init];
   [pModel setValue:[NSString stringWithFormat: @"%.02f",currentPrice] forKey:@"price"];
   [pModel addObserver:self forKeyPath:@"price" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
   
   [pModel setValue:@"0" forKey:@"gold"];
   [pModel addObserver:self forKeyPath:@"gold" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
   
   
   
   [pModel setValue:@"0" forKey:@"quan"];
   [pModel addObserver:self forKeyPath:@"quan" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
   
   
   [pModel setValue:@"0" forKey:@"huo"];
   [pModel addObserver:self forKeyPath:@"huo" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
   
   _shouldPayLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 200, 40)];
   _shouldPayLabel.text=[NSString stringWithFormat:@"应支付: %.02f",[[pModel valueForKey:@"price"] doubleValue]-[studycard doubleValue]- [[pModel valueForKey:@"quan"] doubleValue]];
   _shouldPayLabel.textColor=[UIColor redColor];
   
   label7 =[[UILabel alloc] initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH-20, 30)];
   label7.textAlignment=NSTextAlignmentRight;
   label7.text=[NSString stringWithFormat:@"- 使用大家网论坛金币：￥%@",[pModel valueForKey:@"gold"]];
   
   label6=[[UILabel alloc] initWithFrame:CGRectMake(0, 40+30+30, SCREEN_WIDTH-20, 30)];
   label6.text=[NSString stringWithFormat:@"- 使用优惠劵：￥%@",[pModel valueForKey:@"quan"]];
   label6.textAlignment=NSTextAlignmentRight;
   
   
   label5 =[[UILabel alloc] initWithFrame:CGRectMake(0, 40+30, SCREEN_WIDTH-20, 30)];
   label5.text=[NSString stringWithFormat:@"- 使用活动学习卡：￥%@",[pModel valueForKey:@"huo"]];
   label5.textAlignment=NSTextAlignmentRight;
   
   [infoView addSubview:label5];
   [infoView addSubview:label6];
   [infoView addSubview:label7];
   [payview addSubview:_shouldPayLabel];
   
   item_view_frame = CGRectMake(0, 0.08*SCREEN_WIDTH+80, SCREEN_WIDTH, 80*items.count);
   [self updateItemFrame];
   
}


-(void)studyButton2:(UIButton *)btn{
   
   logdebug(@"OK");
   
   if (btn == self.studyButton ) {
      
      [self.delegate studyCardClick:1];
   }
   
   if (btn == self.studyButton2 ) {
      
      [self.delegate studyCardClick:2];
      
   }
   
}

/**
 *  更新商品位置
 */
- (void)updateItemFrame{
   
   [UIView animateWithDuration:0.3 animations:^{
      itemview.frame = item_view_frame;
   } completion:^(BOOL finished) {
      //
   }];
   [self updateStudyCardFrame];
}
/**
 *  更新学习卡位置
 */
- (void)updateStudyCardFrame{
   card_view_frame = CGRectMake(0, CGRectGetMaxY(item_view_frame)+0.04*SCREEN_WIDTH, SCREEN_WIDTH, card_view_frame.size.height);
   [UIView animateWithDuration:0.3 animations:^{
      //
      studyCardView.frame = card_view_frame;
   }];
   [self updateStudyCardActFrame];
}
/**
 *  更新活动学习卡位置
 */
- (void)updateStudyCardActFrame{
   cardact_view_frame = CGRectMake(0, CGRectGetMaxY(card_view_frame)+3, SCREEN_WIDTH, cardact_view_frame.size.height);
   [UIView animateWithDuration:0.3 animations:^{
      //
      studyCardACTView.frame = cardact_view_frame;
   }];
   [self updateGoldcoinFrame];
}
/**
 *  更细论坛币位置
 */
- (void)updateGoldcoinFrame{
   goldcoin_view_frame = CGRectMake(0, CGRectGetMaxY(cardact_view_frame)+3, SCREEN_WIDTH, goldcoin_view_frame.size.height);
   [UIView animateWithDuration:0.3 animations:^{
      
      goldcoinView.frame = goldcoin_view_frame;
   }];
   
   [self updateCouponFrame];
}
/**
 *  更新折扣券位置
 */
- (void)updateCouponFrame{
   coupon_view_frame = CGRectMake(0, CGRectGetMaxY(goldcoin_view_frame)+3, SCREEN_WIDTH, coupon_view_frame.size.height);
   [UIView animateWithDuration:0.3 animations:^{
      
      couponView.frame = coupon_view_frame;
   }];
   [self updateInfoFrame];
}
/**
 *  更新折扣详情位置
 */
- (void)updateInfoFrame{
   //更新scroll的大小
   info_view_frame = CGRectMake(0, CGRectGetMaxY(coupon_view_frame)+0.04*SCREEN_WIDTH, SCREEN_WIDTH, info_view_frame.size.height);
   [UIView animateWithDuration:0.3 animations:^{
      
      infoView.frame = info_view_frame;
   }];
   
   
   
   scroll.contentSize = CGSizeMake(SCREEN_WIDTH, CGRectGetMaxY(info_view_frame)+40);
}

- (void)showorhidecardact:(UIButton *)btn{
   
   
   if (!btn.selected) {
      
      if (huodongTableArray.count==0) {
         //         cardact_view_frame = CGRectMake(0, cardact_view_frame.origin.y, SCREEN_WIDTH, 120);
         quanTableView.hidden=YES;
         huodongTableView.hidden=YES;
         btn.selected = YES;
      }else{
         huodongTableView.hidden=NO;
         quanTableView.hidden=YES;
         huodongTableView.frame=CGRectMake(SCREEN_WIDTH/3, cardact_view_frame.origin.y+90, SCREEN_WIDTH/2, 120);
         [huodongTableView bringSubviewToFront:self];
         btn.selected = YES;
      }
      
      
      [self updateStudyCardActFrame];
   }else{
      btn.selected = NO;
      quanTableView.hidden=YES;
      huodongTableView.hidden=YES;
      //      cardact_view_frame = CGRectMake(SCREEN_WIDTH/3, cardact_view_frame.origin.y, SCREEN_WIDTH/2, 120);
      [self updateStudyCardActFrame];
   }
}

- (void)showorhidecoupon:(UIButton *)btn{
   
   if (!btn.selected) {
      
      if (quanTableArray.count==0) {
         //        coupon_view_frame = CGRectMake(SCREEN_WIDTH/3, coupon_view_frame.origin.y, SCREEN_WIDTH/2, 120);
         quanTableView.hidden=YES;
         huodongTableView.hidden=YES;
         btn.selected = YES;
      }else{
         quanTableView.hidden=NO;
         huodongTableView.hidden=YES;
         quanTableView.frame=CGRectMake(SCREEN_WIDTH/3, coupon_view_frame.origin.y+30, SCREEN_WIDTH/2, 120);
         btn.selected = YES;
      }
      [self updateCouponFrame];
   }else{
      quanTableView.hidden=YES;
      huodongTableView.hidden=YES;
      btn.selected = NO;
      //      coupon_view_frame = CGRectMake(0, coupon_view_frame.origin.y, SCREEN_WIDTH, 30);
      [self updateCouponFrame];
   }
}


- (void)submitOrder{
   
   NSString *credit;
   if (downGold==0) {
      credit=[NSString stringWithFormat:@"%d",0];
      
   }else{
      if ([credits doubleValue]<=downGold) {
         credit=[NSString stringWithFormat:@"%f",[credits doubleValue]];
         
      }else{
         credit=[NSString stringWithFormat:@"%f",downGold];
         
      }
   }
   
   
   NSString * str =[_shouldPayLabel.text substringFromIndex:4];
   
   if (zcardid==NULL) {
      zcardid=@"0";
      
   }
   if (quanid.count==0) {
      NSArray * arr= @[@0];
      quanid=(NSMutableArray *)arr;
   }
   if (quanval==NULL) {
      quanval=@"0";
   }
   if (credit==NULL) {
      credit=@"0";
   }
   if (downStudycards==NULL) {
      downStudycards=@"0";
   }
   if (zcardval==NULL) {
      zcardval=@"0";
   }
   if (addrid==NULL) {
      ShowAlertMsg(@"请添加收货地址");
      return;
   }
   
   
   
   
   
   
   
   MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
   
   
   
   NSString *module = @"order";
   NSString *interface = @"makeorder";
   
   NSString *timeString = [GetTime getTime];
   NSString *hashString = [[NSString stringWithFormat:@"%@/%@/key/%@/time/%@",module,interface,KEY,timeString] md5];
   NSString *urlString = [NSString stringWithFormat:@"%@/laravel/public/%@/%@/hash/%@/appid/1",URL2,module,interface,hashString];
   NSDictionary *parameter = @{@"time":[GetTime getTime],@"aid":quanArray,@"mid":[UserMessage sharedUserMessage].userMid,@"zcardid":zcardid,@"quanid":quanid,@"quanval":quanval,@"coinnum":[NSString stringWithFormat:@"%d",[credit intValue]],@"bosscount":downStudycards,@"waitpay":str,@"addrid":addrid,@"priceCount":[NSString stringWithFormat:@"%.2f",price],@"price":priceArray,@"source":@"学派课程iOS3.0",@"zcardval":zcardval};
   
//   NSLog(@"%@-------0------0-",parameter);
//   NSLog(@"%@",quanid);
   
   //POST请求
   AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
   manager.responseSerializer = [AFHTTPResponseSerializer serializer];
   [manager POST:urlString parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
      
      NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
      
      NSLog(@"%@",[dic objectForKey:@"info"]);
      
      
      
      if ([[dic objectForKey:@"succ"] isEqualToNumber:@1]) {
         
         
         NSDictionary * info = [dic objectForKey:@"info"];
         
         NSString * oid = [info objectForKey:@"oid"];
         
         //         NSLog(@"%@----",oid);
         
         if ([self.delegate respondsToSelector:@selector(submitOrderwith:andcredits:andoid:)]) {
            [self.delegate submitOrderwith:str andcredits:credit andoid:oid];
         }
         
      }else{
         //         NSLog(@"222");
      }
      [hud hide:YES];
      
   } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
      
      //      NSLog(@"111");
      [hud hide:YES];
   }];
   
   
   
   
   
   
   
   
}



@end
